﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;
using System.Web.UI.HtmlControls;
using System.Text;
using CoolERP_Common;
using System.Collections;

namespace CoolERP_Web.User
{
    public partial class ZL_WL_Edit : BasePage
    {
        static Hashtable htGYS = new Hashtable();
        // 下拉框数组
        private List<DropDownList> listDropDownList;
        // HtmlRow数组
        private static List<TableRowItem> listItems = new List<TableRowItem>();
        private List<Label> listLabel;
        private List<HtmlTableRow> listTableRow;

        protected void Page_Load(object sender, EventArgs e)
        {
            InitAllList();
            if (!IsPostBack)
            {
                BindFirst();
                initValue();
            }
        }

        private void InitAllList()
        {
            listDropDownList = new List<DropDownList>();
            listDropDownList.Add(DropDownList1);
            listDropDownList.Add(DropDownList2);
            listDropDownList.Add(DropDownList3);
            listDropDownList.Add(DropDownList4);
            listDropDownList.Add(DropDownList5);
            listDropDownList.Add(DropDownList6);
            listDropDownList.Add(DropDownList7);
            listDropDownList.Add(DropDownList8);

            listLabel = new List<Label>();
            listLabel.Add(Label1);
            listLabel.Add(Label2);
            listLabel.Add(Label3);
            listLabel.Add(Label4);
            listLabel.Add(Label5);
            listLabel.Add(Label6);
            listLabel.Add(Label7);
            listLabel.Add(Label8);

            listTableRow = new List<HtmlTableRow>();
            listTableRow.Add(tr1);
            listTableRow.Add(tr2);
            listTableRow.Add(tr3);
            listTableRow.Add(tr4);
            listTableRow.Add(tr5);
            listTableRow.Add(tr6);
            listTableRow.Add(tr7);
            listTableRow.Add(tr8);
        }

        private void BindFirst()
        {
            BindWLLX(this.ddlLXBH, "请选择");
            BindJBDW(this.ddlDWBH, "", "请选择");
            BindJBDW(this.ddlJLDWBH, "", "请选择");//初始化计量单位下拉列表
            BindCPYSLX(this.ddlYSLXBH, "请选择");
            using (ZL_GYSBLL gBLL = new ZL_GYSBLL())
            {
                List<ZL_GYS> lsGYS = gBLL.GetAll();                
                foreach (ZL_GYS gys in lsGYS)
                    htGYS[gys.ID.ToString()] = gys.BH;
                this.ddlGYSID.DataSource = lsGYS;
                this.ddlGYSID.DataValueField = "ID";
                this.ddlGYSID.DataTextField = "MC";
                this.ddlGYSID.DataBind();
                this.ddlGYSID.Items.Insert(0, new ListItem("请选择", ""));
            }
        }

        private void initValue()
        {
            object id = this.GetUrlParam("id");
            if (id != null)
            {
                liTitle.Text = "修改";
                this.hdId.Value = id.ToString();
                //修改物料时，物料编号设为不可编辑
                this.tbBH.Enabled = false;
                ZL_WL wl = new ZL_WL();
                using (ZL_WLBLL wlBLL = new ZL_WLBLL())
                {
                    wl = wlBLL.GetEntity(t => t.ID == new Guid(this.hdId.Value));
                }
                if (this.ddlLXBH.Items.FindByValue(wl.LXBH) != null)
                    this.ddlLXBH.Items.FindByValue(wl.LXBH).Selected = true;
                //显示库存单位
                if (this.ddlDWBH.Items.FindByValue(wl.DWBH) != null)
                    this.ddlDWBH.Items.FindByValue(wl.DWBH).Selected = true;
                //显示计量单位
                if (this.ddlJLDWBH.Items.FindByValue(wl.JLDWBH) != null)
                    this.ddlJLDWBH.Items.FindByValue(wl.JLDWBH).Selected = true;
                //显示单位转换系数
                this.tbDWZHXS.Text = wl.DWZHXS.ToString().Trim();
                if (this.ddlYSLXBH.Items.FindByValue(wl.YSLXBH) != null)
                {
                    this.ddlYSLXBH.Items.FindByValue(wl.YSLXBH).Selected = true;
                    InitYS();
                }
                if (this.ddlYSBH.Items.FindByValue(wl.YSBH) != null)
                    this.ddlYSBH.Items.FindByValue(wl.YSBH).Selected = true;
                if (this.ddlGYSID.Items.FindByValue(wl.GYSID.ToString()) != null)
                    this.ddlGYSID.Items.FindByValue(wl.GYSID.ToString()).Selected = true;
                this.tbBH.Text = wl.BH;
                this.tbGG.Text = wl.GG;
                this.tbMC.Text = wl.MC;
                this.hdBh.Value = wl.BH;
                this.tbZDKCSL.Text = wl.ZDKCSL.ToString();
                this.tbZGKCSL.Text = wl.ZGKCSL.ToString();

                #region======初始化属性======
                ShowSXDropDownList(wl.LXBH);
                if (wl.SXMC1 != null && !wl.SXMC1.Trim().Equals("0"))
                    Label1.Text = wl.SXMC1;
                if (wl.SXZBH1 != null && !wl.SXZBH1.Trim().Equals("0") && DropDownList1.Items.FindByValue(wl.SXZBH1.ToString()) != null)
                    DropDownList1.Items.FindByValue(wl.SXZBH1).Selected = true;
                if (wl.SXMC2 != null && !wl.SXMC2.Trim().Equals("0"))
                    Label2.Text = wl.SXMC2;
                if (wl.SXZBH2 != null && !wl.SXZBH2.Trim().Equals("0") && DropDownList2.Items.FindByValue(wl.SXZBH2) != null)
                    DropDownList2.Items.FindByValue(wl.SXZBH2).Selected = true;
                if (wl.SXMC3 != null && !wl.SXMC3.Trim().Equals("0"))
                    Label4.Text = wl.SXMC4;
                if (wl.SXZBH3 != null && !wl.SXZBH3.Trim().Equals("0"))
                {
                    if (DropDownList3.Items.FindByValue(wl.SXZBH3) != null)
                        DropDownList3.Items.FindByValue(wl.SXZBH3).Selected = true;
                }
                if (wl.SXMC4 != null && !wl.SXMC4.Trim().Equals("0"))
                    Label4.Text = wl.SXMC4;
                if (wl.SXZBH4 != null && !wl.SXZBH4.Trim().Equals("0"))
                {
                    if (DropDownList4.Items.FindByValue(wl.SXZBH4) != null)
                        DropDownList4.Items.FindByValue(wl.SXZBH4).Selected = true;
                }
                if (wl.SXMC5 != null && !wl.SXMC5.Trim().Equals("0"))
                    Label5.Text = wl.SXMC5;
                if (wl.SXZBH5 != null && !wl.SXZBH5.Trim().Equals("0"))
                {
                    if (DropDownList5.Items.FindByValue(wl.SXZBH5) != null)
                        DropDownList5.Items.FindByValue(wl.SXZBH5).Selected = true;
                }
                if (wl.SXMC6 != null && !wl.SXMC6.Trim().Equals("0"))
                    Label6.Text = wl.SXMC6;
                if (wl.SXZBH6 != null && !wl.SXZBH6.Trim().Equals("0"))
                {
                    if (DropDownList6.Items.FindByValue(wl.SXZBH6) != null)
                        DropDownList6.Items.FindByValue(wl.SXZBH6).Selected = true;
                }
                if (wl.SXMC7 != null && !wl.SXMC7.Trim().Equals("0"))
                    Label7.Text = wl.SXMC7;
                if (wl.SXZBH7 != null && !wl.SXZBH7.Trim().Equals("0"))
                {
                    if (DropDownList7.Items.FindByValue(wl.SXZBH7) != null)
                        DropDownList7.Items.FindByValue(wl.SXZBH7).Selected = true;
                }
                if (wl.SXMC8 != null && !wl.SXMC8.Trim().Equals("0"))
                    Label1.Text = wl.SXMC1;
                if (wl.SXZBH8 != null && !wl.SXZBH8.Trim().Equals("0"))
                {
                    if (DropDownList8.Items.FindByValue(wl.SXZBH8) != null)
                        DropDownList8.Items.FindByValue(wl.SXZBH8).Selected = true;
                }
                #endregion
            }
        }

        private void InitYS()
        {
            if (!string.IsNullOrEmpty(this.ddlYSLXBH.SelectedValue))
                BindCPYS(this.ddlYSBH, this.ddlYSLXBH.SelectedValue, "请选择");
        }

        protected void ddlYSLXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            InitYS();
        }

        protected void ddlLXBH_SelectedIndexChanged(object sender, EventArgs e)
        {
            tr1.Visible = false;
            tr2.Visible = false;
            tr3.Visible = false;
            tr4.Visible = false;
            tr5.Visible = false;
            tr6.Visible = false;
            tr7.Visible = false;
            tr8.Visible = false;
            // 根据类型获取属性
            ShowSXDropDownList(ddlLXBH.SelectedValue.Trim());
            liLXBH.Text = ddlLXBH.SelectedValue;
        }

        protected void ddlGYSID_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((htGYS != null && htGYS.ContainsKey(ddlGYSID.SelectedValue)))
                liGYSBH.Text = (string)htGYS[ddlGYSID.SelectedValue];
        }

        private void ShowSXDropDownList(string type)
        {
            using (CS_WLLXSXBLL wBLL = new CS_WLLXSXBLL())
            {
                List<CS_WLLXSX> listWLLXSX = wBLL.GetList(t => t.WLLXBH == type);
                listItems.Clear();
                if (listWLLXSX.Count > 0)
                {
                    // 将8个属性下拉框依次初始化
                    for (int i = 0; i < listWLLXSX.Count; i++)
                    {
                        string key = listWLLXSX[i].BH;
                        string value = listWLLXSX[i].MC;
                        DropDownList ddl = listDropDownList[i];
                        Label lbl = listLabel[i];
                        HtmlTableRow tr = listTableRow[i];
                        TableRowItem tri = new TableRowItem(i, key, value, ddl, lbl, tr, this.ddlLXBH.SelectedValue);
                        listItems.Add(tri);
                    }
                    // 显示row
                    foreach (TableRowItem item in listItems)
                        item.Tr.Visible = item.Enable;
                }
                else
                {
                    tr1.Visible = false;
                    tr2.Visible = false;
                    tr3.Visible = false;
                    tr4.Visible = false;
                    tr5.Visible = false;
                    tr6.Visible = false;
                    tr7.Visible = false;
                    tr8.Visible = false;
                }
            }
        }

        protected void btnBH_Click(object sender, EventArgs e)
        {
            // 1、编号是统一长度，15位，其中第一位为数字2代表原料，材料类型2位，供应商4位。
            // 前7位是固定的，后面属性个数是变化的（有的材料属性有5个，个别材料属性只有3个），
            // 每个属性随材料类型而变化，对应的字段长定长2位，但编码长度17不变，没有属性的补充0。
            if (string.IsNullOrEmpty(ddlLXBH.SelectedValue))
            {
                this.lbResult.Text = "请选择物料类型";
                return;
            }
            if (string.IsNullOrEmpty(ddlGYSID.SelectedValue))
            {
                this.lbResult.Text = "请选择供应商";
                return;
            }
            StringBuilder sb = new StringBuilder();
            sb.Append("2");
            string cllx = ddlLXBH.SelectedValue.PadLeft(2, '0');
            sb.Append(cllx);

            string gysbh = "";
            if ((htGYS != null && htGYS.ContainsKey(ddlGYSID.SelectedValue)))
            {
                gysbh = (string)htGYS[ddlGYSID.SelectedValue];
                gysbh = gysbh.Substring(0, 4);
            }
            gysbh = gysbh.PadLeft(4, '0');
            sb.Append(gysbh);
            sb.Append("-");

            lbResult.Text = "";
            this.tbBH.Text = "";
            string BH = sb.ToString();
            int i = 1;
            foreach (HtmlTableRow tr in listTableRow)//获取扩展属性
            {
                HtmlTableRow row = tr as HtmlTableRow;
                if (row.Visible == true && i <= 4)
                {
                    DropDownList drop = row.FindControl("DropDownList" + i.ToString()) as DropDownList;
                    Label label = row.FindControl("Label" + i.ToString()) as Label;
                    if (drop.SelectedValue == "")
                    {
                        lbResult.Text = "请选择" + label.Text;
                        return;
                    }
                    BH += drop.SelectedValue;
                }
                i++;
            }
            BH += "-";
            using (ZL_WLBLL wBLL = new ZL_WLBLL())
            {
                string maxBH = wBLL.GetMaxBH(BH);
                string lsh = "1";//流水号
                int num = 20 - BH.Length;
                if (maxBH == null)//没有该编号
                {
                    lsh = lsh.PadLeft(num, '0');
                }
                else
                {
                    lsh = maxBH.Substring(maxBH.LastIndexOf('-') + 1);
                    int serialNumber = Convert.ToInt32(lsh.TrimStart('0')) + 1;
                    lsh = serialNumber.ToString().PadLeft(num, '0');
                }
                BH += lsh;

                // 检查编号是否已经存在，若存在则提醒
                this.tbBH.Text = BH;
                lbResult.Text = "";
                Guid id = Guid.Empty;
                if (!string.IsNullOrEmpty(this.hdId.Value))
                    id = new Guid(this.hdId.Value);
                if (wBLL.IsExist(t => t.BH == BH && t.ID != id))
                {
                    this.tbBH.Text = "";
                    lbResult.Text = "此编号已经存在，请重新选择";
                }
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            this.lbResult.Text = "";
            if (string.IsNullOrEmpty(this.tbBH.Text.Trim()))
            {
                this.lbResult.Text = "请先生成编号";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlDWBH.SelectedValue))
            {
                this.lbResult.Text = "请选择库存单位";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlJLDWBH.SelectedValue))
            {
                this.lbResult.Text = "请选择计量单位";
                return;
            }
            if (string.IsNullOrEmpty(this.tbDWZHXS.Text.Trim()))
            {
                this.lbResult.Text = "请输入单位转换系数，如果不确定请输入数字1！";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlLXBH.SelectedValue))
            {
                this.lbResult.Text = "请选择物料类型";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlGYSID.SelectedValue))
            {
                this.lbResult.Text = "请选择供应商";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlYSLXBH.SelectedValue))
            {
                this.lbResult.Text = "请选择颜色类型";
                return;
            }
            if (string.IsNullOrEmpty(this.ddlYSBH.SelectedValue))
            {
                this.lbResult.Text = "请选择颜色";
                return;
            }

            using (ZL_WLBLL wBLL = new ZL_WLBLL())
            {
                ZL_WL entity = new ZL_WL();
                string newBh = this.tbBH.Text.Trim();
                Guid id = Guid.Empty;
                if (!string.IsNullOrEmpty(this.hdId.Value))
                {
                    id = new Guid(this.hdId.Value);
                    entity = wBLL.GetEntity(t => t.ID == id);
                }
                else
                    entity.ID = Guid.NewGuid();
                entity.BH = this.tbBH.Text.Trim();
                entity.MC = this.tbMC.Text.Trim();
                entity.GG = this.tbGG.Text.Trim();
                entity.DWBH = this.ddlDWBH.SelectedValue;
                entity.JLDWBH = this.ddlJLDWBH.SelectedValue;
                entity.LXBH = this.ddlLXBH.SelectedValue;
                entity.GYSID = Guid.Parse(this.ddlGYSID.SelectedValue);
                entity.YSLXBH = this.ddlYSLXBH.SelectedValue;
                entity.YSBH = this.ddlYSBH.SelectedValue;
                entity.ZDKCSL = string.IsNullOrEmpty(this.tbZDKCSL.Text.Trim()) ? 0 : double.Parse(this.tbZDKCSL.Text.Trim());
                entity.ZGKCSL = string.IsNullOrEmpty(this.tbZGKCSL.Text.Trim()) ? 0 : double.Parse(this.tbZGKCSL.Text.Trim());
                entity.DWZHXS = string.IsNullOrEmpty(this.tbDWZHXS.Text.Trim()) ? 1 : double.Parse(this.tbDWZHXS.Text.Trim());
                #region======添加属性======
                foreach (TableRowItem item in listItems)
                {
                    if (item.Enable)
                    {
                        int c = item.Index + 1;
                        switch (c)
                        {
                            case 1:
                                entity.SXBH1 = item.Key;
                                entity.SXMC1 = item.Value;
                                entity.SXZBH1 = DropDownList1.SelectedValue;
                                entity.SXZMC1 = DropDownList1.SelectedItem.Text;
                                break;
                            case 2:
                                entity.SXBH2 = item.Key;
                                entity.SXMC2 = item.Value;
                                entity.SXZBH2 = DropDownList2.SelectedValue;
                                entity.SXZMC2 = DropDownList2.SelectedItem.Text;
                                break;
                            case 3:
                                entity.SXBH3 = item.Key;
                                entity.SXMC3 = item.Value;
                                entity.SXZBH3 = DropDownList3.SelectedValue;
                                entity.SXZMC3 = DropDownList3.SelectedItem.Text;
                                break;
                            case 4:
                                entity.SXBH4 = item.Key;
                                entity.SXMC4 = item.Value;
                                entity.SXZBH4 = DropDownList4.SelectedValue;
                                entity.SXZMC4 = DropDownList4.SelectedItem.Text;
                                break;
                            case 5:
                                entity.SXBH5 = item.Key;
                                entity.SXMC5 = item.Value;
                                entity.SXZBH5 = DropDownList5.SelectedValue;
                                entity.SXZMC5 = DropDownList5.SelectedItem.Text;
                                break;
                            case 6:
                                entity.SXBH6 = item.Key;
                                entity.SXMC6 = item.Value;
                                entity.SXZBH6 = DropDownList7.SelectedValue;
                                entity.SXZMC7 = DropDownList7.SelectedItem.Text;
                                break;
                            case 7:
                                entity.SXBH7 = item.Key;
                                entity.SXMC7 = item.Value;
                                entity.SXZBH7 = DropDownList7.SelectedValue;
                                entity.SXZMC7 = DropDownList7.SelectedItem.Text;
                                break;
                            case 8:
                                entity.SXBH8 = item.Key;
                                entity.SXMC8 = item.Value;
                                entity.SXZBH8 = DropDownList8.SelectedValue;
                                entity.SXZMC8 = DropDownList8.SelectedItem.Text;
                                break;
                        }
                    }
                }
                #endregion
                string resultMessage = "";
                try
                {
                    bool result = false;
                    if (string.IsNullOrEmpty(this.hdId.Value))
                        result = wBLL.Insert(entity);
                    else
                        result = wBLL.Update(entity);
                    if (result)
                    {
                        resultMessage = "保存成功";
                    }
                }
                catch (Exception ex)
                {
                    resultMessage = "保存失败" + ex.Message;
                }
                finally
                {
                    this.lbResult.Text = resultMessage;
                }
            }
        }

        /// <summary>
        /// 清空所有TextBox内容
        /// </summary>
        private void ClearControlsValue()
        {
            this.lbResult.Text = "";
            this.tbBH.Text = "";
            this.tbMC.Text = "";
            this.ddlDWBH.SelectedIndex = -1;
            this.ddlJLDWBH.SelectedIndex = -1;
            this.ddlGYSID.SelectedIndex = -1;
            this.ddlLXBH.SelectedIndex = -1;
        }

        protected class TableRowItem
        {
            public int Index { get; set; }
            public HtmlTableRow Tr { get; set; }
            public string Key { get; set; }
            public string Value { get; set; }
            public DropDownList Ddl { get; set; }
            public List<CS_WLSXZ> Data { get; set; }
            public string Wllxbh { get; set; }
            public Label Lbl { get; set; }
            public bool Enable { get; set; }

            public TableRowItem(int index, string key, string value, DropDownList ddl, Label label, HtmlTableRow tr, string wllxbh)
            {
                this.Index = index;
                this.Key = key;
                this.Value = value;
                this.Ddl = ddl;
                this.Wllxbh = wllxbh;
                this.Lbl = label;
                this.Tr = tr;
                Init();
            }

            private void Init()
            {
                this.Lbl.Text = Value;
                // 获取属性值
                using (CS_WLSXZBLL wBLL = new CS_WLSXZBLL())
                {
                    List<CS_WLSXZ> list = wBLL.GetList(t => t.WLLXBH == this.Wllxbh && t.WLSXBH == this.Key);
                    this.Ddl.Items.Clear();
                    if (list.Count > 0)
                    {
                        this.Ddl.DataSource = list;
                        this.Ddl.DataTextField = "MC";
                        this.Ddl.DataValueField = "BH";
                        this.Ddl.DataBind();
                        this.Enable = true;
                    }
                    else
                    {
                        this.Enable = false;
                    }
                }
                this.Ddl.Items.Insert(0, new ListItem("请选择", ""));
            }
        }
    }
}